package com.lanou.tipsy.fragment.recommend;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.recommend.NewRecommendAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.entity.recommend.newest_recommend.newestSongsList.NewestListDetail;
import com.lanou.tipsy.tools.volley.ResponseErrorListener;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by dllo on 15/11/7.
 * <p/>
 * <p/>
 * 推荐  最新歌曲  详情
 */
public class NewRecommendFragment extends BaseFragment implements View.OnClickListener {
    private static final String TAG = "NewRecommendFragment";
    private Context context;
    private NewRecommendAdapter adapter;//适配器
    private SimpleDraweeView draweeView;
    private Toolbar toolbar;
    private VolleySingleton volleySingleton;
    private ResponseErrorListener errorListener;
    private TextView songsNumTv, songsTagTv;//歌曲数量,tag
    private NewestListDetail newestListDetail;
    private ArrayList<NewestListDetail.SonglistEntity> songlistEntities;//实体类
    private RecyclerView newestRv;//列表
    private SimpleDraweeView new_recommend_ic;//顶部图片
    private CollapsingToolbarLayout ctl_new_songs;
    private RequestQueue requestQueue;
    //private SimpleDraweeView draw_new_songs;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_new_recommend, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(getView());
        //获取上个页面传递过来的数据
        Bundle bundle = getArguments();
        String url = (String) bundle.getSerializable("url");
        String newUrl = Constants.NEWET_RECOMMEND_URL + url;
        //拼接新网址
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(newUrl/*url待定*/, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (response != null) {
                    Log.i("test", TAG + response + "");
                    Gson gson = new Gson();
                    String str = response.toString();
                    NewestListDetail newestListDetail = gson.fromJson(str, NewestListDetail.class);
                    songlistEntities = newestListDetail.getSonglist();
                    Log.i("test", TAG + " 最新歌单信息" + songlistEntities + "");
                    //初始化适配器
                    adapter = new NewRecommendAdapter(context, songlistEntities);
                    newestRv.setAdapter(adapter);
                    //设置列表信息
                    newestRv.setLayoutManager(new LinearLayoutManager(context));
                    newestRv.setItemAnimator(new DefaultItemAnimator());
                    //设置标题栏信息
                    songsNumTv.setText("共" + songlistEntities.size() + "首歌");
                    Log.i("test", TAG + "是否有数据" + songlistEntities.size() + "");
                    songsTagTv.setText("");
                    Uri uri = Uri.parse(newestListDetail.getAlbumInfo().getPic_s500());
                    new_recommend_ic.setImageURI(uri);
                    ctl_new_songs.setTitle(newestListDetail.getAlbumInfo().getTitle());

                }
            }
        }, errorListener);
        //requestQueue.add(jsonObjectRequest);
        volleySingleton.addQueue(jsonObjectRequest, TAG);
    }


    //初始化控件->监听
    private void initView(View view) {
        volleySingleton = VolleySingleton.getInstance();//volley
        errorListener = new ResponseErrorListener();
        toolbar = (Toolbar) view.findViewById(R.id.tl_new_songs);
        newestRv = (RecyclerView) view.findViewById(R.id.rv_new_recommend);
        songsNumTv = (TextView) view.findViewById(R.id.tv_new_songs_count);
        songsTagTv = (TextView) view.findViewById(R.id.tv_new_songs_tag);
        ctl_new_songs = (CollapsingToolbarLayout) view.findViewById(R.id.ctl_new_songs);
        new_recommend_ic = (SimpleDraweeView) view.findViewById(R.id.draw_new_songs);
        //draw_new_songs = (SimpleDraweeView) view.findViewById(R.id.draw_new_songs);
        toolbar.setNavigationIcon(R.mipmap.btn_web_back_normal);
        toolbar.setNavigationOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        getActivity().getSupportFragmentManager().popBackStack();
    }

    @Override
    public void onStop() {
        super.onStop();
        volleySingleton.removeRequest(TAG);
    }


}
